<?php

/**
 * uniBoard API Mass Mail
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_mass_mail extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * Mail ID
	 *
	 * @var integer
	 */
	
	public $mail_id 				= 0;
	
	/**
	 * Mail Groups
	 *
	 * @var array
	 */
	
	public $mail_groups				= array();
	
	/**
	 * Mail ignores users mailing prefferences?
	 *
	 * @var bool
	 */
	
	public $mail_everyone			= false;
	
	/**
	 * Mail Subject
	 *
	 * @var string
	 */
	
	public $mail_subject			= '';
	
	/**
	 * Mail Content
	 *
	 * @var string
	 */
	
	public $mail_content			= '';
		
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store Mail ID
		$this -> mail_id		= (integer) $target_id;
		
		// Autoload If Mail ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'mass_mails
			WHERE mail_id = \'' . $this -> mail_id . '\'
			LIMIT 1');
			
			// Mail found
			if ( $mail_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded				= true;
				
				$this -> mail_groups	 			= (array) explode( ',', $mail_result['mail_groups']);
				$this -> mail_everyone	 			= $mail_result['mail_everyone'];
				$this -> mail_subject	 			= $mail_result['mail_subject'];
				$this -> mail_content	 			= $mail_result['mail_content'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_mail_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> mail_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'mass_mails');
			
				// Store new Mail id
				$this -> mail_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'mass_mails', 'mail_id = \'' . $this -> mail_id . '\'');
			}
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Delete data
		$this -> appCore -> db -> delete( 'mass_mails', 'mail_id = \'' . $this -> mail_id . '\'');
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set mail_groups
	 *
	 * @param array $new_value
	 */
	
	function setMailGroups( $new_value){
		
		// Store in Api
		$this -> mail_groups = (array) $new_value;	
	
		// Add to made changes
		$this -> changes['mail_groups'] = $this -> appCore -> strEscape( join( ',', $this -> mail_groups));
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set mail_everyone
	 *
	 * @param bool $new_value
	 */
	
	function setMailEveryone( $new_value){
		
		// Store in Api
		$this -> mail_everyone = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['mail_everyone'] = $this -> mail_everyone;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set mail_subject
	 *
	 * @param string $new_value
	 */
	
	function setMailSubject( $new_value){
		
		// Not empty?
		if ( isset( $new_value[0]))
		{
			// Store in Api
			$this -> mail_subject = $new_value;
			
			// Too long?
			if ( mb_strlen( $new_value) > 255)
			{
				// Set Error
				$this -> setError( $this -> appCore -> lang['acp_mail_subject_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['mail_subject'] = $this -> appCore -> db -> strEscape( $this -> mail_subject);
			}
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_mail_subject_empty']);
		}
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set mail_content
	 *
	 * @param string $new_value
	 */
	
	function setMailContent( $new_value){
		
		// Store in Api
		$this -> mail_content = $new_value;	
	
		// Not empty?
		if ( isset( $new_value[0]))
		{
			// Add to made changes
			$this -> changes['mail_content'] = $this -> appCore -> db -> strEscape( $this -> mail_content);
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_mail_content_empty']);
		}
		
	}
	
}